package com.ilxqx.framework.generator.core.mysql;

import com.google.common.collect.Maps;
import com.ilxqx.framework.generator.constant.GenerationConfigConstants;
import com.ilxqx.framework.generator.core.AbstractCodeGenerator;
import com.ilxqx.framework.generator.core.ModelData;
import com.ilxqx.framework.generator.core.util.TemplateGenerationUtils;
import com.ilxqx.framework.generator.parser.Table;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * 基于mysql的代码生成
 * @author venus
 */
@Slf4j
public class SimpleMysqlBasedCodeGenerator extends AbstractCodeGenerator {

    private ModelData modelData;

    public SimpleMysqlBasedCodeGenerator(ModelData modelData) {
        this.modelData = modelData;
    }

    /**
     * 代码生成
     */
    @Override
    public void generate() {
        // 每一张表都需要生成
        for (String table : GenerationConfigConstants.TABLES) {
            Map<String, Object> data = this.modelData.getModelData(table);
            // 必须要有table数据
            Assert.isTrue(data.containsKey("table"), "必须要有解析后的表数据");
            Table tableInfo = (Table) data.get("table");
            for (String template : GenerationConfigConstants.TEMPLATES) {
                TemplateGenerationUtils.generateJavaCode(data, tableInfo, template, Maps.newHashMap());
            }
        }
    }

}
